import { MaterialService } from './../../../@core/services/material/material.service';
import { Component, ViewChild } from '@angular/core';
import { DataTableComponent, DialogService, TableWidthConfig, TreeComponent } from 'ng-devui';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';
import { StorehouseService } from 'src/app/@core/services/storehouse/storehouse.service';
import { AddMaterialCategroyComponent } from './materialCategory/add-material-categroy/add-material-categroy.component';
import { EditMaterialCategoryComponent } from './materialCategory/edit-material-category/edit-material-category.component';
import { Router } from '@angular/router';
import { concatMap } from 'rxjs';
import { AttachmentComponent } from 'src/app/@shared/components/attachment/attachment.component';
import { environment } from 'src/environments/environment';
@Component({
  selector: 'app-material',
  templateUrl: './material.component.html',
  styleUrls: ['./material.component.scss']
})
export class MaterialComponent {
  [x: string]: any;
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20
  };
  SearchTerm:string=null;
  pageSizeOptions = [10, 20, 30, 50, 100];
  treeData:any;
  allCheckedStatus = false;
  checkedIds = [];
  deleteIDs = [];
  editemployeeId:""
  msgs: Array<Object> = [];
  option:any;//部门option
  pathPids=[]//部门树结构路径
  config = {
    maxHeight: '400px',
    width: '400px',
    backdropCloseable: true,
  }//弹窗配置
  filteredTreeNodes:any
  displayTreeData: any[];
  dataTableOptions = {
    columns: [
      {
        field: 'code',
        header: '物料编码',
        fieldType: 'text',
        fixedLeft:'200px'
      },
      {
        field: 'name',
        header: '物料名称',
        fieldType: 'text',
        fixedLeft:'300px'
      },
      {
        field: 'enName',
        header: '英文名称',
        fieldType: 'string'
      },
      {
        filed:'enabled',
        header:'物料规格',
        fieldType:'boolean'
      },
      {
        filed:'enabled',
        header:'图片',
        fieldType:'boolean'
      },
      {
        filed:'enabled',
        header:'助记码',
        fieldType:'boolean'
      },
      {
        filed:'enabled',
        header:'物料描述',
        fieldType:'boolean'
      },
      {
        filed:'enabled',
        header:'单位',
        fieldType:'boolean'
      },
      {
        filed:'enabled',
        header:'附件',
        fieldType:'boolean'
      },

      {
        filed:'enabled',
        header:'成本价',
        fieldType:'boolean'
      },
      {
        filed:'enabled',
        header:'销售价',
        fieldType:'boolean'
      },
      {
        filed:'enabled',
        header:'最低库存',
        fieldType:'boolean'
      },
      {
        filed:'enabled',
        header:'最高库存',
        fieldType:'boolean'
      },
      {
        filed:'enabled',
        header:'安全库存',
        fieldType:'boolean'
      },
      {
        filed:'enabled',
        header:'是否检验',
        fieldType:'boolean'
      },
      {
        filed:'enabled',
        header:'状态',
        fieldType:'boolean',
        fixedRight:'0'
      }
    ]
  };//表头数据
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'checkbox',
      width: '80px'
    },
    {
      field:'name',
      width:'120px'
    },
    {
      field:'name',
      width:'100px'
    },
    {
      field:'name',
      width:'100px'
    },
    {
      field:'name',
      width:'130px'
    },
    {
      field:'name',
      width:'100px'
    },
    {
      field:'name',
      width:'100px'
    },
    {
      field:'name',
      width:'100px'
    },
    {
      field:'name',
      width:'100px'
    },
    {
      field:'name',
      width:'100px'
    },
    {
      field:'name',
      width:'100px'
    },
    {
      field:'name',
      width:'100px'
    },
    {
      field:'name',
      width:'100px'
    },
    {
      field:'name',
      width:'100px'
    },
    {
      field:'name',
      width:'100px'
    },
    {
      field:'name',
      width:'100px'
    },
    {
      field:'name',
      width:'100px'
    },
    {
      field:'name',
      width:'100px'
    },
  ];//表格宽度配置
  selectoptions = [
    '全部','启用','禁用'
  ]
  selectvalue = '全部'
  tabledata = []
  @ViewChild('basicTree', { static: true }) basicTree!: TreeComponent;
  @ViewChild(DataTableComponent, { static: true }
    ) datatable!: DataTableComponent;//获取table
  displayTableData: any[];
  Status!: string;
  file: any;
  partClassId: any;
  searchMaterialCategoryValue: any;
  constructor(private dialogService: DialogService,private SharedMethodService:SharedMethodService,private StorehouseService:StorehouseService,private MaterialService:MaterialService,private router:Router){}
  ngOnInit(): void {
    this.Status = this.selectvalue
    this.MaterialService.getPartClassesWithChildren(1,999).subscribe((data:any)=>{
      this.treeData=data.items
      this.displayTreeData = [{ partClassName: '全部',partClassId:null}, ...this.treeData];
      this.displayTreeData = this.displayTreeData.map(this.setOpenTrue);
      if( this.treeData.length>0){
        this.MaterialService.getParts(this.partClassId,this.Status,this.SearchTerm, this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
          this.tabledata=data.items;
          this.displayTableData = this.tabledata;
          this.pager.total=data.totalCount;
        })
      }
    })
  }
  afterTreeInit($event: any) {
    if ($event["1"]) {
      $event["1"].data.isActive = true
    }
  }//树形菜单初始化
  getPathPids(node: { partClassId: any; subPartClasses: any; }, targetId: any, path = []) {
    if (node.partClassId === targetId) {
      return [...path, node.partClassId];
    }
    if (node.subPartClasses) {
      for (let subNode of node.subPartClasses) {
        const subPath = this.getPathPids(subNode, targetId, [...path, node.partClassId]);
        if (subPath) {
          return subPath;
        }
      }
    }
    return null;
  } //获取树形菜单pid的路径
  nodeSelected($event:any){
    this.option=$event.data.originItem
    this.partClassId = $event.data.originItem.partClassId;
    this.MaterialService.getParts(this.partClassId,this.Status,this.SearchTerm, this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.tabledata=data.items;
          this.displayTableData = this.tabledata;
          this.pager.total=data.totalCount;
        })
        for(let node of  this.treeData){
          const path = this.getPathPids(node, this.partClassId);
          if(path){
            this.pathPids = path;
            break;
          }
        }
  }//树形菜单点击事件
  CreatCategroy(dialogtype?: string){
    let data = {
      options: this.treeData,
      pathPids: this.pathPids && this.pathPids[0] !== null ? this.pathPids : [],
      display: false
    };
    const results = this.dialogService.open({
      id: 'department',
      ...this.config,
      title: '新建物料类别',
      data:data,
      content: AddMaterialCategroyComponent,
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            if(results.modalContentInstance.MaterialCategory.partClassName){
              this.MaterialService.createPartClass(results.modalContentInstance.MaterialCategory).subscribe((data:any)=>{
                this.showToast('success','成功','物料类别创建成功')
                results.modalInstance.hide();
                this.MaterialService.getPartClassesWithChildren(1,999).subscribe((data:any)=>{
                  this.treeData=data.items
                  this.displayTreeData = [{ partClassName: '全部',partClassId:null}, ...this.treeData];
                  this.displayTreeData = this.displayTreeData.map(this.setOpenTrue)})
              },(error) => {
                if(error.status==404){
                  this.showToast('error', '失败', '服务器连接失败');
                }else{
                  this.showToast('error', '失败', error.error);
                }
                results.modalInstance.hide();
              })
            }
            data.display = true;
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  }//新建分类
  EditCategroy(dialogtype?: string){
    if(this.partClassId){
      let data = {
        options: this.treeData,
        pathPids: this.pathPids.slice(0, -1),
        display: false,
        option:this.option
      };
      const results = this.dialogService.open({
        id: 'department',
        ...this.config,
        title: '编辑物料类别',
        data:data,
        content: EditMaterialCategoryComponent,
        dialogtype: dialogtype,
        onClose: () => {
        },
        buttons: [
          {
            cssClass: 'stress',
            text: '确定',
            handler: ($event: Event) => {
              if(results.modalContentInstance.MaterialCategory.partClassName){
                this.MaterialService.updatePartClass(results.modalContentInstance.MaterialCategory,data.option.partClassId,).subscribe((data:any)=>{
                  this.showToast('success','成功','物料类别编辑成功')
                  results.modalInstance.hide();
                  this.MaterialService.getPartClassesWithChildren(1,999).subscribe((data:any)=>{
                    this.treeData=data.items
                    this.displayTreeData = [{ partClassName: '全部',partClassId:null}, ...this.treeData];
                    this.displayTreeData = this.displayTreeData.map(this.setOpenTrue)})
                },(error) => {
                  if(error.status==404){
                    this.showToast('error', '失败', '服务器连接失败');
                  }else{
                    this.showToast('error', '失败', error.error);
                  }
                  results.modalInstance.hide();
                })
              }
              data.display = true;
            },
          },
          {
            id: 'btn-cancel',
            cssClass: 'common',
            text: '取消',
            handler: ($event: Event) => {
              results.modalInstance.hide();
            },
          },
        ],
      });
    }
  }//编辑分类
  DeleteCategroy(dialogtype?: string){
    if(this.partClassId){
      const results = this.dialogService.open({
        ...this.config,
        title: '删除仓库分类',
        content: '确认删除当前选中的物料类别？',
        dialogtype: dialogtype,
        onClose: () => {
        },
        buttons: [
          {
            cssClass: 'stress',
            text: '确定',
            handler: ($event: Event) => {
              this.MaterialService.deletePartClass(this.partClassId).subscribe((data:any)=>{
                this.showToast('success','成功','该物料类别删除成功')
                results.modalInstance.hide();
                this.MaterialService.getPartClassesWithChildren(1,999).subscribe((data:any)=>{
                  this.treeData=data.items
                  this.displayTreeData = [{ partClassName: '全部',partClassId:null}, ...this.treeData];
                  this.displayTreeData = this.displayTreeData.map(this.setOpenTrue)})
              },(error) => {
                if(error.status==404){
                  this.showToast('error', '失败', '服务器连接失败');
                }else{
                  this.showToast('error', '失败', error.error);
                }
              })
            },
          },
          {
            id: 'btn-cancel',
            cssClass: 'common',
            text: '取消',
            handler: ($event: Event) => {
              results.modalInstance.hide();
            },
          },
        ],
      });
    }
  }//删除分类
  DownloadPartClassFileModel(){
    this.MaterialService.downloadPartClassFileModel().subscribe((data:any)=>{
      const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
      const objectUrl = URL.createObjectURL(blob);
      const a = document.createElement('a');
      document.body.appendChild(a);
      a.setAttribute('style', 'display:none');
      a.setAttribute('href', objectUrl);
      a.setAttribute('download', '物料类型.xlsx');
      a.click();
      URL.revokeObjectURL(objectUrl);
    })
  }//下载物料类型模板
  ImportPartClassesFromExcel(event: any) {
    // 当用户选择了Excel文件时，将其赋值给组件的file属性
    this.file = event.target.files[0];
    this.MaterialService.ImportPartClassesFromExcel(this.file).subscribe((data:any)=>{
          this.showToast('success','成功','导入成功')
          this.MaterialService.getPartClassesWithChildren(1,999).subscribe((data:any)=>{
            this.treeData=data.items
            this.displayTreeData = [{ partClassName: '全部' ,partClassId:null}, ...this.treeData];
          }
          )
        }
        ,(error) => {
          if(error.status==404){
            this.showToast('error', '失败', '服务器连接失败');
          }else{
            this.showToast('error', '失败', error.error);
          }
        })
  }//导入物料类型
  showToast(type: any,summary:any,content:any) {
    switch (type) {
    default:
      this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  }//提示框
  searchMaterialCategory($event:any){
    this.searchMaterialCategoryValue = $event.target.value;
    if (this.searchMaterialCategoryValue === '') {
      // 如果用户输入的值为空，显示原先的数据
      this.displayTreeData = this.displayTreeData = [{ partClassName: '全部',partClassId:null}, ...this.treeData];
    } else {
      this.filteredTreeNodes = this.filterCategories(this.treeData, this.searchMaterialCategoryValue);
      this.displayTreeData = this.filteredTreeNodes
      this.displayTreeData = this.displayTreeData.map(this.setOpenTrue);
    }
  }//搜索仓库类目
  pageIndexChange($event: any) {
    this.pager.pageIndex=$event;
    this.MaterialService.getParts(this.partClassId,this.Status,this.SearchTerm, this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.tabledata=data.items;
          this.displayTableData = this.tabledata;
          this.pager.total=data.totalCount;
        })
  }//切换页码
  pageSizeChange($event: any){
    this.MaterialService.getParts(this.partClassId,this.Status,this.SearchTerm, this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.tabledata=data.items;
          this.displayTableData = this.tabledata;
          this.pager.total=data.totalCount;
        })
  }//改变每页显示数量
  creat(){
    let data = {
      pathPids: this.pathPids && this.pathPids[0] !== null ? this.pathPids : [],
    };
    this.router.navigate(['/pages/basicdata/material/add-material'],{queryParams:data})
  }//新建物料
  disabled(){
    this.MaterialService.disablePart(this.checkedIds).subscribe((data:any)=>{
      this.showToast('success','成功','禁用成功')
      this.deleteIDs = []
      this.MaterialService.getParts(this.partClassId,this.Status,this.SearchTerm, this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
        this.tabledata=data.items;
            this.displayTableData = this.tabledata;
            this.pager.total=data.totalCount;
          }
        )
    },(error) => {
      if(error.status==404){
        this.showToast('error', '失败', '服务器连接失败');
      }else{
        this.showToast('error', '失败', error.error);
      }
    })
  }//禁用物料
  enabled(){
    this.MaterialService.enablePart(this.checkedIds).subscribe((data:any)=>{
      this.showToast('success','成功','启用成功')
      this.deleteIDs = []
      this.MaterialService.getParts(this.partClassId,this.Status,this.SearchTerm, this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
        this.tabledata=data.items;
            this.displayTableData = this.tabledata;
            this.pager.total=data.totalCount;
          }
        )
    },(error) => {
      if(error.status==404){
        this.showToast('error', '失败', '服务器连接失败');
      }else{
        this.showToast('error', '失败', error.error);
      }
    })
  }//启用物料
  delete(id:any,dialogtype?: string ){
    const results = this.dialogService.open({
      id: 'department',
      data: { id: id },
      ...this.config,
      title: '删除仓库',
      content: '确认删除当前选中的物料？',
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            this.MaterialService.deletePart(id).subscribe((data:any)=>{
              this.showToast('success','成功','删除成功')
              this.deleteIDs = []
              results.modalInstance.hide();
              this.MaterialService.getParts(this.partClassId,this.Status,this.SearchTerm, this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
                this.tabledata=data.items;
                    this.displayTableData = this.tabledata;
                    this.pager.total=data.totalCount;
                  }
                )
            },(error) => {
              if(error.status==404){
                this.showToast('error', '失败', '服务器连接失败');
              }else{
                this.showToast('error', '失败', error.error);
              }
            })
            results.modalInstance.hide();
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        }
      ],
    });
  }//删除物料
  deltes(dialogtype?: string){
    const results = this.dialogService.open({
      id: 'department',
      ...this.config,
      title: '删除仓库',
      content: '确认删除当前选中的物料？',
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            this.MaterialService.deleteParts(this.deleteIDs).subscribe((data:any)=>{
              this.showToast('success','成功','删除成功')
              this.deleteIDs = []
              results.modalInstance.hide();
              this.MaterialService.getParts(this.partClassId,this.Status,this.SearchTerm, this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
                this.tabledata=data.items;
                    this.displayTableData = this.tabledata;
                    this.pager.total=data.totalCount;
                  },(error) => {
                    if(error.status==404){
                      this.showToast('error', '失败', '服务器连接失败');
                    }else{
                      this.showToast('error', '失败', error.error);
                    }
                  }
                )

              },(error) => {
                if(error.status==404){
                  this.showToast('error', '失败', '服务器连接失败');
                }else{
                  this.showToast('error', '失败', error.error);
                }
              }
            )
            results.modalInstance.hide();
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        }
      ],
    });
  }//批量删除
  edit(id:any,dialogtype?: string,){
    this.router.navigate(['/pages/basicdata/material/edit-material',id])
  }//编辑物料
  selctChange(){
    this.Status = this.selectvalue
    setTimeout(() => {
      this.MaterialService.getParts(this.partClassId,this.Status,this.SearchTerm, this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
        this.tabledata=data.items;
            this.displayTableData = this.tabledata;
            this.pager.total=data.totalCount;
          })
    }, 100);
  }//下拉框选择
  search(){
    this.pager.pageIndex = 1
    this.MaterialService.getParts(this.partClassId,this.Status,this.SearchTerm, this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.tabledata=data.items;
          this.displayTableData = this.tabledata;
          this.pager.total=data.totalCount;
        })
  }//搜索
  reset(){
    this.Status = null
    this.selectvalue = '全部'
    this.departmentId = null
    this.SearchTerm = ''
    this.MaterialService.getParts(this.partClassId,this.Status,this.SearchTerm, this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
      this.tabledata=data.items;
          this.displayTableData = this.tabledata;
          this.pager.total=data.totalCount;
        })
  }//重置
  DownloadModel(){
    this.MaterialService.DownloadPartFileModel().subscribe((data:any)=>{
      const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
      const objectUrl = URL.createObjectURL(blob);
      const a = document.createElement('a');
      document.body.appendChild(a);
      a.setAttribute('style', 'display:none');
      a.setAttribute('href', objectUrl);
      a.setAttribute('download', '物料模板.xlsx');
      a.click();
      URL.revokeObjectURL(objectUrl);
    })
  }//下载模板
  @ViewChild('fileInput1') fileInput1: any;
  @ViewChild('fileInput2') fileInput2: any;
  onButtonClick() {
    // 当按钮被点击时，触发文件输入的点击事件
    this.fileInput1.nativeElement.click();
  }
  onButtonClick2() {
    // 当按钮被点击时，触发文件输入的点击事件
    this.fileInput2.nativeElement.click();
  }
  importFormExcel(event: any) {
    // 当用户选择了Excel文件时，将其赋值给组件的file属性
    this.file = event.target.files[0];
    this.MaterialService.ImportPartsFromExcel(this.file).pipe(
      concatMap((data: any) => {
        // 显示成功提示
        this.showToast('success', '成功', '导入成功');
        return this.StorehouseService.getWarehouses(this.partClassId, this.Status, this.SearchTerm, this.pager.pageIndex, this.pager.pageSize);
      })
    ).subscribe(
      (data: any) => {
        this.search()
      },
      (error) => {
        if(error.status==404){
          this.showToast('error', '失败', '服务器连接失败');
        }else{
          this.showToast('error', '失败', error.error);
        }
      },
      () => {
        // 无论成功还是失败都清除文件选择
        event.target.value = null;
      }
    );
  }
  DownloadWarehouse(){
    this.MaterialService.DownloadPartFile().subscribe((data:any)=>{
      const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
      const objectUrl = URL.createObjectURL(blob);
      const a = document.createElement('a');
      document.body.appendChild(a);
      a.setAttribute('style', 'display:none');
      a.setAttribute('href', objectUrl);
      a.setAttribute('download', '物料信息.xlsx');
      a.click();
      URL.revokeObjectURL(objectUrl);
    })
  }//下载员工信息
  onRowCheckChange(checked: any, rowIndex: any, nestedIndex: any, rowItem: { $checked: any; $halfChecked: boolean; }){
    this.SharedMethodService.onRowCheckChange(checked, rowIndex, nestedIndex, rowItem,this.datatable)
  }//选中行
  getCheckedRows() {
    this.SharedMethodService.getCheckedRows(this.datatable,'partId').subscribe((data:any)=>{
      this.deleteIDs=data;
    })
    this.SharedMethodService.getCheckedRows(this.datatable,'partId').subscribe((data:any)=>{
      this.checkedIds=data;
    })
  }//获取选中行
  cleared(){
    this.SharedMethodService.cleared(this.deleteIDs,this.tabledata,this.datatable).subscribe((data:any)=>{
      this.deleteIDs=data;
    })
  }//清空选中行
  DownloadCategoryModel(){
    this.StorehouseService.downloadWarehouseCategoryFileModel().subscribe((data:any)=>{
      const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
      const objectUrl = URL.createObjectURL(blob);
      const a = document.createElement('a');
      document.body.appendChild(a);
      a.setAttribute('style', 'display:none');
      a.setAttribute('href', objectUrl);
      a.setAttribute('download', '仓库类型.xlsx');
      a.click();
      URL.revokeObjectURL(objectUrl);
    })
  }//下载仓库类型模板
  ImportCategory(event: any) {
    // 当用户选择了Excel文件时，将其赋值给组件的file属性
    this.file = event.target.files[0];
    this.StorehouseService.importWarehouseCategoriesFromExcel(this.file).subscribe((data:any)=>{
          this.showToast('success','成功','导入成功')
          this.StorehouseService.getWarehouseCategorysWithChildren(this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
            this.treeData=data.items
            this.displayTreeData = [{ warehouseCategoryName: '全部' ,partClassId:null}, ...this.treeData];
            if( this.treeData.length>0){
              this.StorehouseService.getAllWarehouses().subscribe((data:any)=>{
                this.tabledata=data.items;
                this.displayTableData = this.tabledata;
                this.pager.total=data.totalCount;
              })
            }
          })
        },(error) => {
          if(error.status==404){
            this.showToast('error', '失败', '服务器连接失败');
          }else{
            this.showToast('error', '失败', error.error);
          }
        },
        () => {
          // 无论成功还是失败都清除文件选择
          event.target.value = null;
        }
      )
  }

  getImagesArray(fileURL: string): string[] {
    let urls = fileURL.split(',');
    urls = urls.map(url => environment.modifiedApiUrl + url);
    return urls;
  }
  fujian(rowitem:any,dialogtype?:string) {
    const results = this.dialogService.open({
      title: '物料附件',
      width: '800px',
      backdropCloseable: false,
      data: {
        data: '物料附件',
        urls: rowitem.addFileURL,
        type: '查看',
      },
      content: AttachmentComponent,
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  }
  filterCategories(departments: any[], keyword: string): any[] {
    const result: any[] = [];
    for (const department of departments) {
      if (department.partClassName.includes(keyword)) {
        result.push(department);
      }
      if (department.subPartClasses.length > 0) {
        const filteredSubCategories = this.filterCategories(department.subPartClasses, keyword);
        if (filteredSubCategories.length > 0) {
          result.push(...filteredSubCategories);
        }
      }
    }
    return result;
  }
  setOpenTrue = (node: { open: boolean; subPartClasses: any[]; }) => {
    // 为当前节点添加 open: true 属性
    node.open = true;
    // 如果当前节点有子节点，递归处理子节点
    if (Array.isArray(node.subPartClasses)) {
      node.subPartClasses = node.subPartClasses.map(this.setOpenTrue);
    }
    return node;
  }
}
